// SPDX-License-Identifier: GPL-2.0
/*
 * lombo_mtdnand.c - lombo mtdnand driver code for LomboTech
 * lombo mtdnand driver driver code implement
 *
 * Copyright (C) 2016-2018, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>

#include <mach/debug.h>
#undef MOD_NAME
#define MOD_NAME	"LOMBO_MTDNAND_IF"

#include "include/nand_phy.h"
#include "lombo_mtdnand_if.h"

int _lombo_nand_read_oob(loff_t from, struct mtd_oob_ops *ops)
{
	int ret = 0;

	if (ops->ooblen > 0) {
		PRT_ERR("ops->ooblen=%d\n", ops->ooblen);
		return -EINVAL;
	}

	if (ops->len > 0) {
		if (ops->datbuf == NULL) {
			PRT_ERR("\n");
			return -EIO;
		}

		ret = nandphy_read(from, ops->len, ops->datbuf);
		if (ret != 0) {
			PRT_ERR("\n");
			return -EIO;
		}

		ops->retlen = ops->len;
	}

	return 0;
}

int _lombo_nand_write_oob(loff_t to, struct mtd_oob_ops *ops)
{
	int ret = 0;

	if (ops->ooblen > 0) {
		PRT_ERR("ops->ooblen=%d\n", ops->ooblen);
		return -EINVAL;
	}

	if (ops->len > 0) {
		if (ops->datbuf == NULL) {
			PRT_ERR("\n");
			return -EIO;
		}

		ret = nandphy_write_data(to, ops->len, ops->datbuf);
		if (ret != 0) {
			PRT_ERR("\n");
			return -EIO;
		}

		ret = nandphy_flush();
		if (ret != 0) {
			PRT_ERR("\n");
			return -EIO;
		}

		ops->retlen = ops->len;

	}

	return 0;
}

unsigned int _lombo_nand_get_oob_size(void)
{
	return 0;
}

int _lombo_nand_erase(unsigned int block)
{
	int ret = 0;

	ret = nandphy_erase_block(block);
	if (ret != 0)
		return -EIO;

	return 0;
}

int _lombo_nand_sync(void)
{
	return nandphy_flush();
}

unsigned int _lombo_nand_get_page_size(void)
{
	return nandphy_get_page_size();
}

unsigned int _lombo_nand_get_block_size(void)
{
	return nandphy_get_block_size();
}

unsigned int _lombo_nand_get_block_cnt(void)
{
	return nandphy_get_capacity() / (nandphy_get_block_size() / 512);
}
